<template>
  <div class="home-container">
    <!-- 顶部导航栏 -->
    <home-header />

    <!-- 主要内容区 -->
    <div class="home-content">
      <div class="content-wrapper">
        <transition-group name="fade-up" tag="div" class="content-left">
          <div key="title" class="title-wrapper">
            <h1 class="main-title">专业的简谱编辑工具</h1>
            <p class="main-subtitle">高效、简洁、易用的在线简谱编辑解决方案</p>
          </div>
          
          <!-- 功能特性展示 -->
          <div key="features" class="features-grid">
            <div v-for="(feature, index) in features" 
                 :key="feature.title" 
                 class="feature-item"
                 :style="{ animationDelay: `${index * 0.2}s` }">
              <i :class="feature.icon"></i>
              <h3>{{ feature.title }}</h3>
              <p>{{ feature.description }}</p>
            </div>
          </div>
        </transition-group>

        <!-- 右侧预览图 -->
        <transition-group name="slide-left" tag="div" class="content-right">
          <div key="action-card" class="right-card action-card-group">
            <div class="card-title start-title">快速开始</div>
            <div class="action-btn-group">
              <div v-for="(action, index) in actions" 
                   :key="action.text" 
                   class="action-btn-block"
                   :style="{ animationDelay: `${index * 0.15}s` }">
                <div class="action-btn-desc left-align">{{ action.description }}</div>
                <el-button :type="action.type" 
                          size="large" 
                          class="action-btn" 
                          @click="action.handler">
                  <i :class="action.icon"></i> {{ action.text }}
                </el-button>
              </div>
            </div>
          </div>
          <div key="help-card" class="right-card help-card">
            <div class="card-title">常见问题与技巧</div>
            <ul class="card-list">
              <li v-for="(item, index) in helpList" 
                  :key="item.id" 
                  class="card-list-item"
                  :style="{ animationDelay: `${index * 0.1}s` }"
                  @click="goToHelpArticle(item.route)">
                {{ item.title }}
              </li>
            </ul>
          </div>
        </transition-group>
      </div>

      <!-- 谱子列表 -->
      <div class="sheet-music-section">
        <h2 class="section-title">热门曲谱</h2>
        <div class="sheet-music-grid">
          <div v-for="(sheet, index) in sheetMusicList" 
               :key="sheet.id" 
               class="sheet-music-item"
               :style="{ animationDelay: `${index * 0.1}s` }"
               @click="goToPreview(sheet.id)">
            <i class="el-icon-video-play sheet-icon"></i>
            <div class="sheet-info">
              <h3 class="sheet-title">{{ sheet.title }}</h3>
              <p class="sheet-artist">{{ sheet.artist }}</p>
            </div>
            <i class="el-icon-arrow-right arrow-icon"></i>
          </div>
        </div>
      </div>

      <!-- 学习笔记列表 -->
      <div class="sheet-music-section" style="margin-top: 30px;">
        <h2 class="section-title">学习笔记</h2>
        <div v-if="recentNotes.length > 0" class="sheet-music-grid">
          <div v-for="(note, index) in recentNotes" 
               :key="note.id" 
               class="sheet-music-item"
               :style="{ animationDelay: `${index * 0.1}s` }"
               @click="goToNote(note.id)">
            <i class="el-icon-notebook-1 sheet-icon"></i>
            <div class="sheet-info">
              <h3 class="sheet-title">{{ note.title }}</h3>
              <p class="sheet-artist">{{ formatDate(note.date) }}</p>
            </div>
            <i class="el-icon-arrow-right arrow-icon"></i>
          </div>
        </div>
        <el-empty v-else description="暂无学习笔记" :image-size="120" />
      </div>
    </div>

    <!-- 底部信息 -->
    <transition name="fade-up">
      <div class="home-footer">
        <div class="footer-content">
          <span class="version-info">Version 1.0.0</span>
          <span class="copyright">© 2024 双手简谱编辑器. All rights reserved.</span>
        </div>
      </div>
    </transition>

    <create-score-dialog :visible.sync="showCreateScore" />
  </div>
</template>

<script>
import HomeHeader from '../components/HomeHeader.vue'
import CreateScoreDialog from '../components/CreateScoreDialog.vue'

export default {
  name: 'HomeView',
  components: { 
    HomeHeader,
    CreateScoreDialog 
  },
  data() {
    return {
      showCreateScore: false,
      helpList: [
        { id: 1, title: '如何新建和保存乐谱？', route: '/help/1' },
        { id: 2, title: '如何导入/导出简谱文件？', route: '/help/2' },
        { id: 3, title: '快捷键与高效编辑技巧', route: '/help/3' },
        { id: 4, title: '如何添加歌词和和弦？', route: '/help/4' }
      ],
      sheetMusicList: [
        { id: 1, title: '小星星', artist: '莫扎特' },
        { id: 2, title: '致爱丽丝', artist: '贝多芬' },
        { id: 3, title: '月光奏鸣曲', artist: '贝多芬' },
        { id: 4, title: '土耳其进行曲', artist: '莫扎特' },
        { id: 5, title: '欢乐颂', artist: '贝多芬' },
        { id: 6, title: '蓝色多瑙河', artist: '约翰·施特劳斯' }
      ],
      recentNotes: [],
      features: [
        {
          icon: 'el-icon-edit-outline',
          title: '双声部编辑',
          description: '支持左右手双声部同时编辑，轻松创作钢琴曲谱'
        },
        {
          icon: 'el-icon-video-play',
          title: '实时试听',
          description: '内置钢琴音色，随时试听编辑效果'
        },
        {
          icon: 'el-icon-document',
          title: '智能排版',
          description: '自动调整布局，支持多种拍号格式'
        },
        {
          icon: 'el-icon-share',
          title: '便捷分享',
          description: '一键导出，支持多种格式分享'
        }
      ],
      actions: [
        {
          type: 'primary',
          icon: 'el-icon-plus',
          text: '新建谱子',
          description: '快速创建一份新的简谱乐谱，支持双声部和歌词。',
          handler: () => this.showCreateScore = true
        },
        {
          type: 'info',
          icon: 'el-icon-view',
          text: '预览版',
          description: '',
          handler: this.goPreview
        },
        {
          type: 'success',
          icon: 'el-icon-star-on',
          text: '专业版',
          description: '',
          handler: this.goPro
        },
        {
          type: 'warning',
          icon: 'el-icon-microphone',
          text: '录音版',
          description: '',
          handler: this.goRecord
        },
        {
          type: 'danger',
          icon: 'el-icon-piano',
          text: '自由弹奏',
          handler: this.goFreePlay
        },
        {
          type: 'primary',
          icon: 'el-icon-guide',
          text: '指法跟弹',
          handler: this.goFingeringFollow
        }
      ]
    }
  },
  created() {
    this.loadRecentNotes()
  },
  methods: {
    loadRecentNotes() {
      const savedNotes = localStorage.getItem('studyNotes')
      if (savedNotes) {
        const notes = JSON.parse(savedNotes)
        // 按日期排序并只取前6条
        this.recentNotes = notes
          .sort((a, b) => new Date(b.date) - new Date(a.date))
          .slice(0, 6)
      }
    },
    formatDate(date) {
      const d = new Date(date)
      const pad = n => n.toString().padStart(2, '0')
      return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
    },
    goToNote(id) {
      this.$router.push(`/note-detail?id=${id}`)
    },
    goPreview() {
      this.$router.push('/preview')
    },
    goPro() {
      this.$router.push('/pro')
    },
    goImport() {
      this.$message.info('导入功能开发中...')
    },
    goHelp() {
      this.showHelp()
    },
    showHelp() {
      this.$message({
        message: '帮助文档正在编写中...',
        type: 'info'
      })
    },
    goToHelpArticle(route) {
      this.$router.push(route)
    },
    goRecord() {
      this.$router.push('/record')
    },
    goToPreview(id) {
      this.$router.push(`/preview/${id}`)
    },
    goFreePlay() {
      this.$router.push('/free-play')
    },
    goFingeringFollow() {
      this.$router.push('/fingering-follow')
    }
  }
}
</script>

<style scoped>
.home-container {
  background: linear-gradient(135deg, #f7f8fa 0%, #eef2f7 100%);
  display: flex;
  flex-direction: column;
}

.home-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 90px; /* header高度，调整为90px */
  max-width: 1400px;
  margin-left: auto;
  margin-right: auto;
  padding: 40px;
}

.content-wrapper {
  display: flex;
  gap: 60px;
  width: 100%;
}

.content-left {
  flex: 1;
  max-width: 600px;
}

.main-title {
  font-size: 2.5rem;
  font-weight: 700;
  color: #222;
  margin-bottom: 16px;
  line-height: 1.2;
  white-space: nowrap;
  text-align: center;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.main-subtitle {
  font-size: 1.2rem;
  color: #666;
  margin-bottom: 48px;
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 24px;
  margin-bottom: 48px;
}

.feature-item {
  background: #fff;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.05);
  transition: transform 0.3s ease;
  position: relative;
  overflow: hidden;
}

.feature-item:hover {
  transform: translateY(-4px);
}

.feature-item::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(45deg, transparent, rgba(255,255,255,0.1), transparent);
  transform: translateX(-100%);
  transition: transform 0.6s ease;
}

.feature-item:hover::after {
  transform: translateX(100%);
}

.feature-item i {
  font-size: 24px;
  color: #409EFF;
  margin-bottom: 16px;
}

.feature-item h3 {
  font-size: 1.1rem;
  font-weight: 600;
  color: #222;
  margin-bottom: 8px;
}

.feature-item p {
  font-size: 0.9rem;
  color: #666;
  line-height: 1.5;
}

.content-right {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  min-width: 240px;
}

.right-card {
  width: 240px;
  background: #fff;
  border-radius: 7px;
  box-shadow: 0 1px 4px #e0e7ef;
  padding: 12px 14px 8px 14px;
  margin-bottom: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.card-title {
  font-size: 12px;
  font-weight: 600;
  color: #222;
  margin-bottom: 5px;
  letter-spacing: 0.2px;
}

.card-list {
  width: 100%;
  list-style: none;
  padding: 0;
  margin: 0;
}

.card-list-item {
  font-size: 12px;
  color: #222;
  padding: 8px 0;
  border-bottom: 1px solid #f3f3f3;
  cursor: pointer;
  transition: color 0.2s;
  line-height: 1.5;
  animation: fadeIn 0.5s ease forwards;
  opacity: 0;
}

.card-list-item:last-child {
  border-bottom: none;
}

.card-list-item:hover {
  color: #409eff;
}

.home-footer {
  background: #fff;
  padding: 24px;
  text-align: center;
}

.footer-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
  color: #999;
  font-size: 0.8rem;
}

.action-card-group {
  margin-bottom: 0;
  padding-bottom: 14px;
}

.action-btn-group {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.action-btn-block {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 2px;
  margin-bottom: 3px;
}

.action-btn-block:last-child {
  margin-bottom: 0;
}

.action-btn-desc {
  font-size: 11px;
  color: #666;
  margin-bottom: 0;
  margin-top: 0;
  line-height: 1.4;
  padding-bottom: 2px;
}

.action-btn {
  width: 100%;
  font-size: 0.9rem;
  font-weight: 500;
  height: 32px !important;
  line-height: 30px !important;
  padding: 0 0;
  transition: all 0.3s ease;
  margin-bottom: 12px;
}

.action-btn:last-child {
  margin-bottom: 0;
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.start-title {
  font-size: 16px;
  font-weight: 700;
  color: #222;
  text-align: left;
  margin-bottom: 4px;
}

.left-align {
  text-align: left;
}

@media (max-width: 1024px) {
  .home-content {
    flex-direction: column;
    margin-top: 64px;
    padding: 20px;
  }
  
  .content-left {
    max-width: 100%;
  }
  
  .features-grid {
    grid-template-columns: 1fr;
  }
  
  .main-title {
    font-size: 2.5rem;
  }
}

@media (max-width: 768px) {
  .main-title {
    font-size: 2rem;
  }
  
  .main-subtitle {
    font-size: 1rem;
  }
}

/* 动画相关样式 */
.fade-up-enter-active,
.fade-up-leave-active {
  transition: all 0.5s ease;
}

.fade-up-enter,
.fade-up-leave-to {
  opacity: 0;
  transform: translateY(20px);
}

.slide-left-enter-active,
.slide-left-leave-active {
  transition: all 0.5s ease;
}

.slide-left-enter,
.slide-left-leave-to {
  opacity: 0;
  transform: translateX(20px);
}

/* 特性卡片动画 */
.feature-item {
  animation: fadeInUp 0.6s ease forwards;
  opacity: 0;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 按钮动画 */
.action-btn {
  transition: all 0.3s ease;
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

/* 帮助列表项动画 */
.card-list-item {
  animation: fadeIn 0.5s ease forwards;
  opacity: 0;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateX(-10px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* 标题动画 */
.title-wrapper {
  animation: titleFadeIn 0.8s ease forwards;
}

@keyframes titleFadeIn {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式动画调整 */
@media (max-width: 768px) {
  .fade-up-enter,
  .fade-up-leave-to {
    transform: translateY(10px);
  }
  
  .slide-left-enter,
  .slide-left-leave-to {
    transform: translateX(10px);
  }
}

/* 谱子列表样式 */
.sheet-music-section {
  width: 100%;
  padding: 24px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.05);
}

.section-title {
  font-size: 1.4rem;
  font-weight: 600;
  color: #222;
  margin-bottom: 20px;
}

.sheet-music-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.sheet-music-item {
  display: flex;
  align-items: center;
  padding: 12px;
  background: #f8fafc;
  border-radius: 8px;
  border: 1px solid #ebeef5;
  cursor: pointer;
  transition: all 0.3s ease;
}

.sheet-music-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  border-color: #409eff;
}

.sheet-icon {
  font-size: 20px;
  color: #409eff;
  margin-right: 12px;
}

.sheet-info {
  flex: 1;
}

.sheet-title {
  font-size: 1rem;
  font-weight: 500;
  color: #222;
  margin: 0 0 4px;
}

.sheet-artist {
  font-size: 0.85rem;
  color: #666;
  margin: 0;
}

.arrow-icon {
  color: #909399;
  font-size: 14px;
  margin-left: 8px;
  transition: all 0.3s ease;
}

.sheet-music-item:hover .arrow-icon {
  color: #409eff;
  transform: translateX(4px);
}

@media (max-width: 1024px) {
  .sheet-music-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .sheet-music-grid {
    grid-template-columns: 1fr;
  }
  
  .section-title {
    font-size: 1.2rem;
  }
}
</style>
